<?php
namespace Shopmobile\Controller;
use Think\Controller;
class ExtendController extends Controller {
	public $user;
	public function  __construct (){
		parent::__construct();
		Vendor("taobaosdk.top.TopClient");
		Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
		Vendor("taobaosdk.top.RequestCheckUtil");
		$sid = I("sid");
		if($sid){
			cookie("sid",$sid);
			$this->sid = $sid;
			//cookie保存sid，读取不到则跳出
		}else{
			$this->sid = cookie("sid");
		}
		if(!$this->sid){
			// 没有店铺信息不能访问
			$this->assign("msg","抱歉，本活动只能在店铺内访问");
			$this->display("Index:not_allow");
			die();
		}
		$this->mixednick = I("tida_mixnick");
		if(!$this->mixednick){
			$this->assign("login",true);
			$this->assign("url",C("URL"));
			$this->assign("msg","正在载入，请在弹出的对话框点击“授权”");
			$this->display("Index:not_allow");
			die();
		}
		// $this->user = S($this->mixednick);
		if(!$this->user){
			$USR = D("UserView");
			$USR->mixednick = $this->mixednick;
			$USR->sid = $this->sid;
			$USR->getUser();
			$this->user = $USR->user_info;
			$this->sys_uid = $USR->user_info["sys_uid"];
			$this->shop_uid = $USR->user_info["shop_uid"];
			S($this->mixednick,$this->user);
		}		
		$friend = I("friend");
		$FIR = D("Friendships");
		$FIR->sid = $this->sid;
		$FIR->home_id = $this->shop_uid;
		$FIR->home_mixednick = $this->mixednick;
		// $codeimg = ($FIR->get_code_img());

// print_r($codeimg);
		if($friend){
			$FIR->home_id=null;
			$FIR->guest_id = $this->shop_uid;
			$FIR->home_mixednick = $friend;
			$FIR->guest_mixednick = $this->mixednick;
			$FIR->invite(); //邀请逻辑
		}
		
		$this->assign("appkey",C("FT_KEY"));
		$this->assign("appname",cookie("appname"));
		$this->assign("codeimg",$codeimg);
		$this->assign("sid",$this->sid);
		$this->assign("user",$this->user);
	}
	public function ajaxReturn($reda){ 
		$reda = json_encode($reda);
		$re = I("get.callback")?I("get.callback").'('.$reda.')':$reda;
		header("KissyIoDataType:json");		
		header("Access-Control-Allow-Origin: ".C("Allow_Origin"));
		echo $re;
	}
}